package main

import (
	_ "baseLearning/base1/pkg" //引入某个包，但不直接使用包里的函数，而是调用该包里面的init函数
	"encoding/json"
	"fmt"
)

func mainStruct() {

	var ac Account = Account{
		money: 21.9,
		User: User{
			Name:     "王五",
			Age:      21,
			password: "98756",
		},
	}

	fmt.Printf("ac %#v, %T\n", ac, ac)

	var u1 = User{
		Name:     "lisa",
		Age:      22,
		password: "1233fg",
	}
	var ac1 Account = Account{
		money: 21.9,
		User:  u1,
	}
	fmt.Printf("ac1 %#v, %T\n", ac1, ac1)

	fmt.Println(ac.money)
	fmt.Println(ac.Name)
	fmt.Println(ac.User.Name)
	fmt.Println(ac.password)

	user := User{"王二麻子", 23, "xsdfe23"}
	fmt.Printf("main: %p\n", &user)
	name := user.PrintName() // 是值传递，
	fmt.Println(name, user)

	fmt.Println("==================")
	var u User = User{
		Name: "zhangsan",
	}
	u.Age = 20
	fmt.Println(u)
	var u2 *User = &User{
		Name: "李四",
	}
	u2.Age = 21
	fmt.Println(u2)

	fmt.Printf("u = %+v, type of %T\n", u, u)
	fmt.Printf("u2 = %+v, type of %T\n", u2, u2)

	fmt.Println("==================")
	article := Article{
		Title:     "golang文档",
		Desc:      "golang零基础入门，四小时转岗golang开发",
		Content:   "golang零基础入门，四小时转岗golang开发",
		Username:  "fengfeng",
		LookCount: 1024,
		password:  "ksd8%38",
		Free:      true,
	}

	// 结构体转json
	jsonData, err := json.Marshal(article)
	if err != nil {
		fmt.Println(err)
		return
	}
	jsonStr := string(jsonData)
	fmt.Println(jsonStr)
	u6 := User{
		Name: "张三",
	}
	u6.SetName("EMINEM")
	fmt.Printf("setName运行完之后，u=%+v\n", u6)
	//如果需要修改原结构体，那么需要传递的是结构体的指针
	u6.SetName2("Jayz")
	fmt.Printf("setName2运行完之后，u=%+v\n", u6)
}

type User struct {
	Name     string
	Age      int
	password string
}

type Account struct {
	User
	money float32
	Name  string
}

func (u User) PrintName() string {
	u.Name = "张三"
	fmt.Println("printName方法： ", u)
	fmt.Printf("printName方法内部：%p \n", &u)
	return u.Name
}
func (u User) SetName(name string) {
	u.Name = name
	fmt.Printf("setName修改之后：u=%+v\n", u)
}
func (u *User) SetName2(name string) {
	(*u).Name = name
	// 也可这样简写
	//u.Name = name
	fmt.Printf("setName修改之后：u=%+v\n", *u)
}

type Article struct {
	Title     string `json:"title"`
	Desc      string `json:"desc"`
	Content   string `json:"content"`
	Username  string `json:"-"` // - 也不参与序列化
	LookCount int    `json:"look_count"`
	Free      bool   `json:"free"`
	password  string // 小写字母开头的不会参与序列化
}
